import 'package:cher_flare/utils/app_color.dart';
import 'package:cher_flare/utils/ly_use_log_manager.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LYActionSheetPage extends StatefulWidget {
  final List<String> titles;
  final Function(int) onTap;
  final Function()? onCancelTap;
  const LYActionSheetPage(
      {super.key, required this.titles, required this.onTap, this.onCancelTap});

  @override
  State<LYActionSheetPage> createState() => _LYActionSheetPageState();
}

class _LYActionSheetPageState extends State<LYActionSheetPage> {
  @override
  void initState() {
    super.initState();
    if (widget.titles.length == 3) {
      LYUseLogManager.stat(page: LYLogPages.block);
    } else {
      LYUseLogManager.stat(page: LYLogPages.report);
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> list = [];
    widget.titles.asMap().forEach((index, title) {
      list.add(_makeItemWidget(title, index));
      list.add(_makeLine());
    });
    list.add(_makeCancelWidget());
    return Column(
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Container(
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              color: AppColors.whiteColor),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisSize: MainAxisSize.min,
            children: list,
          ),
        ),
      ],
    );
  }

  Widget _makeItemWidget(String title, index) {
    return InkWell(
      onTap: () {
        Get.close(0);
        widget.onTap(index);
      },
      child: Container(
        height: 56,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
        ),
        alignment: Alignment.center,
        child: Text(
          title,
          style: const TextStyle(fontSize: 16, color: AppColors.color142220),
        ),
      ),
    );
  }

  Widget _makeCancelWidget() {
    return Container(
      height: 103,
      alignment: Alignment.center,
      child: InkWell(
        onTap: () {
          Get.close(0);
        },
        child: Container(
          width: 180,
          height: 44,
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(22),
              gradient: const LinearGradient(
                  colors: [AppColors.colorCCFF78, AppColors.color78FF85])),
          alignment: Alignment.center,
          child: const Text(
            'Cancel',
            style: TextStyle(fontSize: 16, color: AppColors.color404040),
          ),
        ),
      ),
    );
  }

  Widget _makeLine() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 20),
      height: 0.5,
      color: AppColors.colorEDEDED,
    );
  }
}
